﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using cmdb.keede.common;
using cmdb.keede.data;
using cmdb.keede.entity;
using cmdb.keede.service.Contract;
using Microsoft.EntityFrameworkCore;

namespace cmdb.keede.service
{
    public class VirtualHostInfoService : IVirtualHostInfoService
    {
        readonly IVirtualHostInfoRepository _virtualHostInfoRepository;
        readonly IUnitOfWork _unitOfWork;

        public VirtualHostInfoService(IUnitOfWork unitOfWork, IVirtualHostInfoRepository virtualHostInfoRepository)
        {
            _unitOfWork = unitOfWork;
            _virtualHostInfoRepository = virtualHostInfoRepository;
        }

        public async Task<BizResult<List<VirtualHostInfo>>> GetVirtualHostInfoesAsync(VirtualHostInfoRequest request,  int pageIndex, int pageSize)
        {
            //var query = _virtualHostInfoRepository.GetQueryable();
            //query.Where(e => string.IsNullOrEmpty(request.AssetId) || e.AssetId.Contains(request.AssetId));

            //var result = await query.ToListAsync();

            return await _virtualHostInfoRepository.GetPageAsync(new Pager(pageIndex, pageSize), 
                w => (string.IsNullOrEmpty(request.AssetId) || w.AssetId.Contains(request.AssetId))
                && (string.IsNullOrEmpty(request.Area) || w.Area.Contains(request.Area))
                && (request.DeviceType == 0 || w.DeviceType == request.DeviceType)
                && (request.OS == 0 || w.OS == request.OS)
                && (string.IsNullOrEmpty(request.OSVersion) || w.OSVersion.Contains(request.OSVersion))
                && (string.IsNullOrEmpty(request.InnerIP) || w.InnerIP.Contains(request.InnerIP))
                && (string.IsNullOrEmpty(request.OuterIP) || w.OuterIP.Contains(request.OuterIP))
                && (request.HardDiskSize == 0 || w.HardDiskSize == request.HardDiskSize)
                && (request.RAM == 0 || w.RAM == request.RAM)
                && (request.Status == 0 || w.Status == request.Status)
                && (request.CPU == 0 || w.CPU == request.CPU)
                && (request.ExpireTimeStart.Ticks == 0 || w.ExpireTime > request.ExpireTimeStart)
                && (request.ExpireTimeEnd.Ticks == 0 || w.ExpireTime < request.ExpireTimeEnd)
                , o => o.Id, false);
        }

        public async Task<BizResult<VirtualHostInfo>> GetVirtualHostInfoAsync(int id)
        {
            var obj = await _virtualHostInfoRepository.GetByIdAsync(id);
            return new BizResult<VirtualHostInfo>(obj);
        }

        public async Task<BizResult<bool>> DeleteVirtualHostInfoAsync(int id)
        {
            var obj = await _virtualHostInfoRepository.GetByIdAsync(id);
            obj.Status = EnumVirtualHostStatus.Destroyed;
            await _unitOfWork.CommitAsync();
            return new BizResult<bool>(true);
        }

        public async Task<BizResult<bool>> AddOrUpdateAppInfoAsync(VirtualHostInfo virtualHostInfo)
        {
            if (virtualHostInfo.Id == 0)
            {
                virtualHostInfo.DeviceType = EnumDeviceType.VirtualHost;
                _virtualHostInfoRepository.Add(virtualHostInfo);
            }
            else
            {
                var obj = await _virtualHostInfoRepository.GetByIdAsync(virtualHostInfo.Id);
                obj.Area = virtualHostInfo.Area;
                obj.CPU = virtualHostInfo.CPU;
                obj.AssetId = virtualHostInfo.AssetId;
                //obj.DeviceType = virtualHostInfo.DeviceType;
                obj.ExpireTime = virtualHostInfo.ExpireTime > DateTime.Now ? virtualHostInfo.ExpireTime : obj.ExpireTime;
                obj.HardDiskSize = virtualHostInfo.HardDiskSize;
                obj.InnerIP = virtualHostInfo.InnerIP;
                obj.Maintainers = virtualHostInfo.Maintainers;
                obj.NetworkBandwidth = virtualHostInfo.NetworkBandwidth;
                //obj.OS = virtualHostInfo.OS;
                obj.OuterIP = virtualHostInfo.OuterIP;
                obj.Password = virtualHostInfo.Password;
                obj.RAM = virtualHostInfo.RAM;
                obj.Remark = virtualHostInfo.Remark;
                obj.AdminName = virtualHostInfo.AdminName;
                obj.Status = virtualHostInfo.Status;
            }
            try
            {
                await _unitOfWork.CommitAsync();
            }
            catch (Exception e)
            {
                throw;
            }

            return new BizResult<bool>(true);
        }
    }
}
